package com.snowflake.ecatalog.service.profile;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Maps;
import com.snowflake.ecatalog.contant.CompanyLaunchType;
import com.snowflake.ecatalog.contant.ResourceInfoType;
import com.snowflake.ecatalog.contant.Status;
import com.snowflake.ecatalog.entity.CompanyLaunch;
import com.snowflake.ecatalog.entity.Goods;
import com.snowflake.ecatalog.entity.GoodsCategory;
import com.snowflake.ecatalog.entity.ResourceInfo;
import com.snowflake.ecatalog.repository.CompanyLaunchDao;
import com.snowflake.ecatalog.repository.GoodsCategoryDao;
import com.snowflake.ecatalog.repository.GoodsDao;
import com.snowflake.ecatalog.repository.ResourceInfoDao;
import com.snowflake.ecatalog.vo.LatestData;

@Service
@Transactional(readOnly = true)
public class LatestService {
	@Autowired
	private GoodsCategoryDao goodsCategoryDao;
	@Autowired
	private GoodsDao goodsDao;
	@Autowired
	private CompanyLaunchDao companyLaunchDao;
	@Autowired
	private ResourceInfoDao resourceInfoDao;

	public LatestData getLatestData(Long profileId) {
		LatestData latestData = new LatestData();
		latestData.setProfileId(profileId);
		setCompanyLaunchData(latestData);
		setGoodsCategoryData(latestData);
		setGoodsData(latestData);
		return latestData;
	}

	private void setCompanyLaunchData(LatestData latestData) {
		List<CompanyLaunch> companyLaunchs = companyLaunchDao.findByProfileIdAndStatusOrderByIdAsc(
				latestData.getProfileId(), Status.ENABLE);
		if (CollectionUtils.isNotEmpty(companyLaunchs)) {
			// 设置ResourceInfo
			setCompanyLaunchResourceInfo(companyLaunchs);
			for (CompanyLaunch companyLaunch : companyLaunchs) {
				switch (companyLaunch.getType()) {
				// 广告图片
				case CompanyLaunchType.STARTUP:
					latestData.getStartup().add(setCompanyLaunchMapByResourceInfo(companyLaunch));
					break;
				// 背景图片
				case CompanyLaunchType.BACKGROUND:
					latestData.setBackground(setCompanyLaunchMapByResourceInfo(companyLaunch));
					break;
				// 公司简介
				case CompanyLaunchType.INTRODUCTION:
					latestData.getIntroduction().add(setCompanyLaunchMapByResourceInfo(companyLaunch));
					break;
				// 公司图片
				case CompanyLaunchType.ENVIRONMENT:
					latestData.getEnvironment().add(setCompanyLaunchMapByResourceInfo(companyLaunch));
					break;
				// 联系信息
				case CompanyLaunchType.CONTRACT:
					latestData.setContract(setCompanyLaunchMapByResourceInfo(companyLaunch));
					break;
				}
			}
		}
	}

	private void setGoodsData(LatestData latestData) {
		List<Goods> goodsList = goodsDao.findByCategoryProfileIdAndStatus(latestData.getProfileId(), Status.ENABLE);
		if (CollectionUtils.isNotEmpty(goodsList)) {
			// 设置ResourceInfo
			setGoodsResourceInfo(goodsList);
			for (Goods goods : goodsList) {
				latestData.getGoods().add(setGoodsMapByResourceInfo(goods));
			}
		}
	}

	private void setGoodsCategoryData(LatestData latestData) {
		List<GoodsCategory> goodsCategories = goodsCategoryDao.findByProfileIdAndStatus(latestData.getProfileId(),
				Status.ENABLE);
		for (GoodsCategory goodsCategory : goodsCategories) {
			latestData.getGoodscategory().add(setGoodsCategoryMapByGoodsCategory(goodsCategory));
		}
	}

	// 根据ResourceInfo设置Map
	private Map<String, String> setCompanyLaunchMapByResourceInfo(CompanyLaunch companyLaunch) {
		Map<String, String> map = null;
		if (null != companyLaunch && CollectionUtils.isNotEmpty(companyLaunch.getResourceInfos())) {
			map = Maps.newHashMap();
			List<ResourceInfo> resourceInfos = companyLaunch.getResourceInfos();
			// 通过获取第一个ResourceInfo，找到对应的fileName，如果有"_"，就截取，返回path和Name
			setPathAndFileNameByResourceInfo(map, resourceInfos);
		}
		return map;
	}

	// 通过获取第一个ResourceInfo，找到对应的fileName，如果有"_"，就截取，返回path和Name
	private void setPathAndFileNameByResourceInfo(Map<String, String> map, List<ResourceInfo> resourceInfos) {
		if (CollectionUtils.isNotEmpty(resourceInfos)) {
			ResourceInfo resourceInfo = resourceInfos.get(0);
			String fileName = resourceInfo.getFileName();
			if (StringUtils.isNotBlank(fileName)) {
				if (fileName.indexOf("_") != -1) {
					fileName = fileName.substring(0, fileName.lastIndexOf("_"));
					resourceInfo.setFileName(fileName);
				} else if (fileName.indexOf(".") != -1) {
					fileName = fileName.substring(0, fileName.lastIndexOf("."));
					resourceInfo.setFileName(fileName);
				}
			}
			map.putAll(resourceInfo.getImgSrcMap());
		}
	}

	// 根据ResourceInfo设置Map
	private Map<String, String> setGoodsMapByResourceInfo(Goods goods) {
		Map<String, String> map = null;
		if (null != goods && CollectionUtils.isNotEmpty(goods.getResourceInfos())) {
			map = Maps.newHashMap();
			List<ResourceInfo> resourceInfos = goods.getResourceInfos();
			map.put("id", String.valueOf(goods.getId()));
			map.put("category", String.valueOf(goods.getCategory().getId()));
			// 通过获取第一个ResourceInfo，找到对应的fileName，如果有"_"，就截取，返回path和Name
			setPathAndFileNameByResourceInfo(map, resourceInfos);
		}
		return map;
	}

	// 根据GoodsCategory设置Map
	private Map<String, String> setGoodsCategoryMapByGoodsCategory(GoodsCategory goodsCategory) {
		Map<String, String> map = null;
		if (null != goodsCategory) {
			map = Maps.newHashMap();
			map.put("id", String.valueOf(goodsCategory.getId()));
			map.put("name", goodsCategory.getName());
		}
		return map;
	}

	// 设置ResourceInfo
	private void setCompanyLaunchResourceInfo(List<CompanyLaunch> companyLaunchs) {
		if (CollectionUtils.isNotEmpty(companyLaunchs)) {
			Map<Long, CompanyLaunch> map = Maps.newHashMap();
			for (CompanyLaunch companyLaunch : companyLaunchs) {
				map.put(companyLaunch.getId(), companyLaunch);
			}
			List<ResourceInfo> resourceInfos = resourceInfoDao.findByOwnerIdInAndOwnerType(map.keySet(),
					ResourceInfoType.LAUNCH);
			for (ResourceInfo resourceInfo : resourceInfos) {
				map.get(resourceInfo.getOwnerId()).getResourceInfos().add(resourceInfo);
			}
		}
	}

	// 设置ResourceInfo
	private void setGoodsResourceInfo(List<Goods> goodsList) {
		if (CollectionUtils.isNotEmpty(goodsList)) {
			Map<Long, Goods> map = Maps.newHashMap();
			for (Goods goods : goodsList) {
				map.put(goods.getId(), goods);
			}
			List<ResourceInfo> resourceInfos = resourceInfoDao.findByOwnerIdInAndOwnerType(map.keySet(),
					ResourceInfoType.GOODS);
			for (ResourceInfo resourceInfo : resourceInfos) {
				map.get(resourceInfo.getOwnerId()).getResourceInfos().add(resourceInfo);
			}
		}
	}

}
